using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace hemsidan.change
{
	/// <summary>
	/// Summary description for ManagePageText.
	/// </summary>
	public partial class ManagePageText : Bases.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!IsPostBack)
				LoadData();
		}

		private void LoadData()
		{
			DataTable pageTexts = Entities.PageText.GetAll();
			dgPageTexts.DataSource = pageTexts;
			dgPageTexts.DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{   
			this.dgPageTexts.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgPageTexts_EditCommand);
			this.dgPageTexts.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgPageTexts_DeleteCommand);

		}
		#endregion

		protected void btnSave_Click(object sender, System.EventArgs e)
		{
			if (txtPageTextId.Text == "0")
			{
				Entities.PageText.CreatePageText(txtPageName.Text, txtHeader.Text, txtText.Text);
			}
			else
			{
				Entities.PageText.UpdatePageText(int.Parse(txtPageTextId.Text),txtPageName.Text, txtHeader.Text, txtText.Text);
			}

			ClearEditPart();
		
			LoadData();
		}

		private void dgPageTexts_DeleteCommand(object source, DataGridCommandEventArgs e)
		{
			int PageTextId = Convert.ToInt32(dgPageTexts.DataKeys[e.Item.ItemIndex]);
			Entities.PageText.DeletePageText(PageTextId);
			LoadData();
		}

		private void dgPageTexts_EditCommand(object source, DataGridCommandEventArgs e)
		{
			int PageTextId = Convert.ToInt32(dgPageTexts.DataKeys[e.Item.ItemIndex]);
			txtPageTextId.Text = PageTextId.ToString();
			txtPageName.Text = e.Item.Cells[0].Text;
			txtHeader.Text = e.Item.Cells[1].Text;
			txtText.Text = e.Item.Cells[2].Text;
		}


		protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			ClearEditPart();
		}

		private void ClearEditPart()
		{
			txtPageTextId.Text = "0";
			txtPageName.Text = "";
			txtHeader.Text = "";
			txtText.Text = "";
		}
	}
}
